(function($){
    function Progressbar(div){
        var target = this.target = $(div);
        this.default = {
            percent: 0,
            minColor: "rgba(12,255,255,0.6)",
            maxColor: "rgba(255,0,0,1)",
        }
        this.option = this.parseOption($(div).attr("option"));
        this.init();
        target.data("dyProgressBar" ,this);
    }

    Progressbar.prototype = {
        init:function(){
            //将内层显示进度的div添加至节点内，并给与样式
            var target = this.target.append('<div class="inner"></div>');
            this.inner = target.find(".inner");
            //获取新的option
            var option = this.option = $.extend({},this.default,this.option);
            //执行一次show()
            this.show(option.percent);
        },
        parseOption:function(option){
            if(option && option != ""){
                var opts = {};
                var keyValues = option.split(";");
                for(var index in keyValues){
                    var keyValue = keyValues[index].split(":");
                    opts[keyValue[0]] = keyValue[1];
                }
                return opts;
            }
        },
        show:function(percent){
            var inner = this.inner;
            inner.get(0).style.width = percent+"%";
            inner.get(0).style.backgroundColor = this.calculateColor(percent);
        },
        calculateColor:function(percent){
            var minColor = this.option.minColor;
            var minColorSplits = minColor.split(",");
            var maxColor = this.option.maxColor;
            var maxColorSplits = maxColor.split(",");
            var minRed = minColorSplits[0].split("(")[1],
                minGreen = minColorSplits[1],
                minBlue = minColorSplits[2],
                minAlpha = minColorSplits[3].substring(0 , minColorSplits[3].length - 1),
                maxRed = maxColorSplits[0].split("(")[1],
                maxGreen = maxColorSplits[1],
                maxBlue = maxColorSplits[2],
                maxAlpha = maxColorSplits[3].substring(0 , maxColorSplits[3].length - 1);
            //计算各个颜色通道的差值
            var redDiff = maxRed - minRed;
            var greenDiff = maxGreen - minGreen;
            var blueDiff = maxBlue - minBlue;
            var alphaDiff = maxAlpha - minAlpha;
            var calcuPercent = (100 - percent)/100;
            var calcuColor = "rgba("+Math.round(maxRed - calcuPercent * redDiff) +","+Math.round(maxGreen - calcuPercent * greenDiff) +","+
                Math.round(maxBlue - calcuPercent * blueDiff) +","+(maxAlpha - calcuPercent * alphaDiff)+")";
            return calcuColor;
        }
    }

    $.fn.extend({
        dyProgressBar:function(options, param){
            if (typeof options == 'string'){
                var method = $.fn.dyProgressBar.methods[options];
                if (method){
                    return method(this, param);
                }else{
                    return;
                }
            }

            this.each(function(){
                new Progressbar(this);
            })
            return this;
        }
    });

    $.fn.dyProgressBar.methods = {
        show : function(jq, percent){
            jq.each(function(){
                var dyProgressBar = $(this).data("dyProgressBar");
                var inner = dyProgressBar.inner;
                inner.get(0).style.width = percent+"%";
                inner.get(0).style.backgroundColor = dyProgressBar.calculateColor(percent);
            });
        }
    }
})(jQuery)

//页面加载完毕后去扫描页面元素
$(function(){
    $(".dy-progressbar").dyProgressBar();
});